<?php

/**
 * class provides functions to create html output for the group page
 *
 * @author voellenk
 */

class symsearchpage extends htmlpage {

  public  $form;
  public  $showresult;
  public  $error;
  public  $showdboverview;
  public  $showexchangeoverview;
  public  $showsymdetails;
  public  $currentsym;
  public  $availableexchanges;
  private $mysql;
  
  public function presetFormVariables() {    
    $this->showsymdetails=false;
    $this->showexchangeoverview=false;    
    $this->showdboverview=true;
    $this->form["symstring"] = "";
    $this->form["symbol"] = "";
    $this->form["exchange"] = "";
  }
  
  public function parseGetVariable($GET){
    if (array_key_exists ('symstring',$GET)){
      $this->form["symstring"] = urldecode ($GET["symstring"]);
      if ($this->form["symstring"] != "") {
        $this->currentsym=$this->form["symstring"];
        $this->showsymdetails=true;
      }
    } else {
      $this->form["symstring"]="";
    }
    
    if (array_key_exists ('symbol',$GET)){
      $this->form["symbol"] = urldecode ($GET["symbol"]);
      if ($this->form["symbol"] != "" & !$this->currentsym) {
        $this->currentsym=$this->form["symbol"];
        $this->showsymdetails=true;        
      }
    } else {
      $this->form["symbol"]="";
    }
    
    if (array_key_exists ('exchange',$GET)){
      $ex = urldecode ($GET["exchange"]);
      $exex = explode("-", $ex);
      if (array_key_exists(1, $exex)) {
        $this->form["exchange"] = trim($exex[1]);
        if (!$this->showsymdetails) $this->showexchangeoverview=true;
      } else {
        $this->form["exchange"] = "";
        if (!$this->showsymdetails) $this->showdboverview=true;
      }
    } else {
      $this->form["exchange"] ="";
    }

  }
 
  public function getGeneralDbInfos($mysql) {
    $this->mysql=$mysql;
    $this->availableexchanges = $this->mysql->get_exchanges();
    if ($this->form["exchange"] == "") {
      $this->form["availablesymbols"] = array();
    } else {
      // use selected database
      $this->form["availablesymbols"] = $this->mysql->get_symbols($this->form["exchange"]);      
    }
  }
  
  public function titleBar(){
    $html= "<form name='MENU' method='get' action='".$_SERVER["PHP_SELF"]."'>\n";
    $html.= "<table>";

    //Menu Titlebar
    //exchange
    $html.= "<tr><th>Exchange</th>\n";
    //symbol
    $html.= "<th>Symbol</th>\n";
    //link to Main Page
    $html.= "<th><a href ='index.php'>Main Page</a></th>\n";
    $html.="</tr>\n";

    //SELECT exchange
    $html.= "<tr><td><select name='exchange' size='1'>\n";
    $html.= "<option> </option>\n";
    foreach ($this->availableexchanges as $exchange) {
      if ($exchange["name"] == $this->form["exchange"]) {
        $html.= "<option selected='selected'>".$exchange["id"]." - ".$exchange["name"]."</option>\n";
      } else {
        $html.= "<option>".$exchange["id"]." - ".$exchange["name"]."</option>\n";
      }
    }
    $html.= "</select></td>\n";
    
    //SELECT symbol
    $html.= "<td><select name='symbol' size='1'>\n";
    $html.= "<option> </option>\n";
    foreach ($this->form["availablesymbols"] as $symbol) {
      if ($symbol["code"] == $this->form["symbol"]) {
        $html.= "<option selected='selected'>".$symbol["code"]."</option>\n";
      } else {
        $html.= "<option>".$symbol["code"]."</option>\n";
      }
    }   
    $html.= "</select>";
    
    //INPUT symstring
    $html.= "<input name='symstring' type='text' size='7' maxlength='20' value='".$this->form["symstring"]."' />";
    $html.= "</td>\n";
    
    //GO Button
    $html.= "<td>\n<input type='submit' name='go' value='OK' /></td>\n";
    $html.= "</tr>\n";
    $html.= "</table>\n";
    $html.= "</form>\n";
    $html.= "<hr />\n";
    return htmlpage::addStyle($html, htmlpage::$styleTM);
  }
  
  public function symdetails() {
    $html = "<h2>details for ".$this->currentsym."</h2>\n";
    $html.="<table><tr><th>id</th><th>code</th><th>name</th><th>exchange</th>";
    $html.="<th>from</th><th>to</th><th># obs</th><th>group</th></tr>\n";
    //could be more than one symbol with the same name 
    $symarr= $this->mysql->get_symbol($this->currentsym);
    foreach ($symarr as $symbol) {
      $ex=$this->mysql->get_exchange($symbol["id"]);
      $fromto=$this->mysql->get_date_boundaries($symbol["id"]);
      $noobs=$this->mysql->count_observations($symbol["id"]);
      $html.="<tr><td>".$symbol["id"]."</td><td>".$this->currentsym."</td><td>".$symbol["name"]."</td>";
      $html.="<td>".$ex["id"]." - ".$ex["name"]."</td>";
      $html.="<td>".$fromto[0]."</td><td>".$fromto[1]."</td><td>".current($noobs)."</td>";
      $html.="<td>none</td></tr>\n";
    }
    $html.="</table>\n";    
    return htmlpage::addStyle($html, htmlpage::$styleOV);    
  }
  
  public function dboverview() {
    $html ="<h2>database overview</h2>\n";
    $html.="<table><tr><th>exchange name</th><th>number of symbols</th></tr>\n";
    foreach ($this->availableexchanges as $exchange) {
      $nosym=$this->mysql->count_symbols($exchange["name"]);
      $html.="<tr><td>";
      $html.="<a href='".$_SERVER["PHP_SELF"]."?exchange=-".$exchange["name"]."'>".$exchange["name"]."</a></td>";
      $html.="<td>".current($nosym)."</td></tr>\n";
    }
    $html.="</table>\n";
    return htmlpage::addStyle($html, htmlpage::$styleOV);
  }
  
  public function exchangeoverview() {
    $html ="<h2>".$this->form["exchange"]." overview</h2>\n";   
    $html.="<table><tr><th>id</th><th>code</th><th>name</th><th>from</th><th>to</th>";
    $html.="<th># obs</th></tr>\n"; 
    foreach ($this->form["availablesymbols"] as $symbol) {
      $html.="<tr><td>".$symbol["id"]."</td>";
      $html.="<td><a href='".$_SERVER["PHP_SELF"]."?symstring=".$symbol["code"]."'>".$symbol["code"]."</a>";
      $html.="</td><td>".$symbol["name"]."</td>";
      $fromto=$this->mysql->get_date_boundaries($symbol["id"]);
      $noobs=$this->mysql->count_observations($symbol["id"]);
      $html.="<td>".$fromto[0]."</td><td>".$fromto[1]."</td><td>".current($noobs)."</td></tr>\n";
    }   
    $html.="</table>\n";    
    return htmlpage::addStyle($html, htmlpage::$styleOV);    
  }

  
}

?>
